package com.xxgc.demo.controller;

import com.xxgc.demo.controller.pojo.PictureRecyclingPOJO;
import com.xxgc.demo.controller.result.Result;
import com.xxgc.demo.controller.vo.NewsVO;
import com.xxgc.demo.utils.RedisUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Tag(name = "新闻")
@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private RedisUtil redisUtil;

    @Operation(summary = "添加新闻",description = "新闻")
    @PostMapping("/addNews")

    public Result addNews(@RequestBody NewsVO newsVO){
        //----------------清除图片垃圾回收---------------------
        //获取图片地址
        String coverImage = newsVO.getCoverImage();
        //截取uuid
        String substring = coverImage.substring(coverImage.lastIndexOf('/') + 1, coverImage.lastIndexOf("."));
        //拼接key
        String key = "recycling:image:"+substring;
        //拿到回收对象
        PictureRecyclingPOJO prPO = (PictureRecyclingPOJO) redisUtil.get(key);
        //清除这张图的缓存
        redisUtil.lRemove("junk_images",1,prPO);
        redisUtil.del(key);
        //-------------------正常逻辑------------------------


        return Result.ok().msg("添加成功");
    }

}